/*
 * @Author: szx
 * @Date: 2022-03-19 14:11:56
 * @LastEditTime: 2022-03-19 14:23:44
 * @Description:
 * @FilePath: \leetcode\700-799\738\738_2.js
 */
/**
 * @param {number} n
 * @return {number}
 */
var monotoneIncreasingDigits = function (n) {
    // 什么神仙做法
    const arr = n
        .toString()
        .split('')
        .map((v) => +v);
    let i = 0;
    while (i < arr.length && arr[i - 1] <= arr[i]) {
        i += 1;
    }
    if (i < arr.length) {
        while (i > 0 && arr[i - 1] >= arr[i]) {
            arr[i - 1] -= 1;
            i--;
        }
        while (i < arr.length) {
            arr[i++] = 9;
        }
    }
    return Number.parseInt(arr.join(''));
};
